package test.org.nanotek.jpa.persistence.artist;

import javax.validation.constraints.NotNull;

import org.nanotek.Builder;
import org.nanotek.base.artist.Artist;
import org.nanotek.base.artist.ArtistType;
import org.nanotek.base.artist.name.ArtistName;
import org.nanotek.base.person.name.NamePart;
import org.nanotek.base.person.name.NamePersonType;

import test.org.nanotek.jpa.name.namepart.NamePartBuilder;
import test.org.nanotek.jpa.persistence.person.PersonBuilderException;

public class ArtistBuilder implements Builder<Artist>{

	private Artist artist = new Artist(); 
	private int consistence;
	
	public ArtistBuilder() {
	}

	public ArtistBuilder setArtistType()
	{ 
		artist.setArtistType(ArtistType.Actor);
		return this;
	}
	
	public ArtistBuilder setGivenName (String value) 
	{ 
		artist.getNamePerson().getName().add(new NamePartBuilder().setName(value, NamePersonType.GivenName).build());
		consistence = consistence | NamePersonType.GivenName.ordinal();
		return this;
	}
	
	public ArtistBuilder setLastName (String value) 
	{ 
		artist.getNamePerson().getName().add(new NamePartBuilder().setName(value, NamePersonType.LastName).build());
		consistence = consistence | NamePersonType.LastName.ordinal();
		return this;
	}

	public ArtistBuilder setMiddleName (String value) 
	{ 
		NamePart np = new NamePartBuilder().setName(value, NamePersonType.MiddleName).build();
		artist.getNamePerson().getName().add(np);
		return this;
	}
	
	public ArtistBuilder addArtistName (@NotNull ArtistName artistName) 
	{ 
		artist.getArtistNames().add(artistName);
		return this;
	}
	
	
	@Override
	public Artist build() {
		checkConstraint();
		return artist;
	}
	
	protected void checkConstraint()
	{ 
		 int t = (consistence);
		 int ver = (NamePersonType.GivenName.ordinal() | NamePersonType.LastName.ordinal());
		 System.out.println(ver + "  " + t);
		 if (t < ver) 
			 throw new PersonBuilderException("InConsistent Record");
		 System.out.println(t);
	}
	
}
